/**
 * 种植界面
 */
class plantSub extends eui.Component{
    private return:eui.Button;
    private nickName:eui.Label;//植物名称
    private energy:eui.Label;//当前经验值
    private show:eui.Image;//植物显示
    private index:eui.Label;//当前植物索引
    private project:eui.Rect;
    private delete:eui.Button;//铲除
    private invite:eui.Button;//邀请好友浇水
    private useEnergy:eui.Button;//使用能量浇水
    private useWater:eui.Button;//浇水
    private toLeft:eui.Button;
    private toRight:eui.Button;
    private harvest:eui.Button;
    private addAll:eui.Button;

    //引导
    private guideGroup:eui.Group;
    private guideToWater:eui.Group;
    private guideToEnergy:eui.Group;

    private _model:model;

    private _index:number = 0;
    private _name:string = "";
    private _show:string = "";//展示资源
    private _id:number = 0;//土地id
    private _plantId:number = 0;//植物种植表id
    private _curGrow:number = 0;//当前成长值
    private _maxGrow:number = 0;//最大成长值
    private _growStage:string = "";//当前阶段
    private _userId:number = 0;
    private _toUserId:number = 0;
    private _userPlant:any;
    private _node:number;//0:主页面植物详情,1:其他页面植物详情

    private _action:egret.MovieClipDataFactory;
    private _water:egret.MovieClip;
    private _energy:egret.MovieClip;
    private energyAction:eui.Image;
    private _energyNote:number = 0;

    private _time;
    public constructor(index,name,show,id,pId,curGrow,maxGrow,growStage,node:number = 0,userId:number = 0){
        super();
        this._index = index;
        this._name = name;
        this._show = show;
        this._id = id;
        this._plantId = pId;
        this._curGrow = curGrow;
        this._maxGrow = maxGrow;
        this._growStage = growStage;
        if(node == 0){
            this._userPlant = common.userPlant;
        }else{
            this._userPlant = common.otherPlant;
            this._userId = userId;
        }
        this._toUserId = userId;
        this._node = node;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "plantSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.Init();
    }

    private Init(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,this.closePage,this);

        // Dispatcher.ins.addEventListener("plantDetail",this.resetValue,this);
        Dispatcher.ins.addEventListener("toWater",this.assignValue,this);
        Dispatcher.ins.addEventListener("getHarvest",this.closePage,this);
        Dispatcher.ins.addEventListener("deleteBotany",this.closePage,this);

        this.initData();

        this.delete.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.invite.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.useEnergy.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this ,false);
        this.useWater.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.toLeft.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.toRight.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );
        this.addAll.addEventListener( egret.TouchEvent.TOUCH_TAP, this.mbtnHandler, this );

        if(this._node !== 0){
            this.delete.visible = false;
            this.invite.visible = false;
            this.useEnergy.visible = false;
            this.addAll.visible = false;
        }
        this.harvest.addEventListener(egret.TouchEvent.TOUCH_TAP,this.mbtnHandler, this )

        if(common.userInfo.login == 1){
            this.toGuide();
        }
    }
    private mbtnHandler(evt:egret.TouchEvent):void{
        let btn = evt.currentTarget;
        switch(btn){
            case this.addAll:{
                let diff = this._maxGrow - this._curGrow;
                if(diff == 0){
                    Dispatcher.ins.dispatchEvent(new GameEvent("toast","当前植物已成熟，可收取"));
                    return;
                }
                let compare = diff - common.userInfo.energy;
                if(compare > 0){
                    //不能够加满
                    let _model:model = new model("当前植物加满需" + diff + "能量值,当前用户能量值"+common.userInfo.energy+"是否去商店购买能量值？");
                    _model.addEventListener("delete",()=>{
                        this.removeChild(_model);
                        let energy:energyStore = new energyStore();
                         energy.addEventListener("return",()=>{
                             this.removeChild(energy);
                         },this);
                         this.addChild(energy);
                    },this);
                    this.addChild(_model);
                }else{
                    //能够加满
                    let _model:model = new model("当前植物加满需" + diff + "能量值,当前用户能量值"+common.userInfo.energy+"是否直接加满？");
                    _model.addEventListener("delete",()=>{
                        this.removeChild(_model);
                        let dataPost = {
                            "plantId":this._plantId,
                            "toUserId":this._toUserId,
                            "wType":2
                        }
                        socket.request("toWater",dataPost,dataPost.wType);
                    },this);
                    this.addChild(_model);
                }
                break;
            }
            case this.delete:{
                //展示选择是否删除页面
                // if(!this._model){
                //     this._model = new model("铲除后该植物将彻底消失，确认铲除该植物吗？");
                //     this._model.addEventListener("delete",()=>{
                //         this.removeChild(this._model);
                //         let dataPost = {
                //             "id":this._plantId
                //         }
                //         socket.request("deleteBotany",dataPost);
                //     },this);
                // }
                // this.addChild(this._model);
                let _model:model = new model("铲除后该植物将彻底消失，确认铲除该植物吗？");
                _model.addEventListener("delete",()=>{
                        this.removeChild(_model);
                        let dataPost = {
                            "id":this._plantId
                        }
                        socket.request("deleteBotany",dataPost);
                },this);
                this.addChild(_model);
                break;
            }
            case this.invite:{
                Dispatcher.ins.dispatchEvent(new GameEvent("invite"));
                break;
            }
            case this.useEnergy:{
                if(common.userInfo.energy == 0){
                    let _model:model = new model("能量不足，是否需要购买？");
                    _model.addEventListener("delete",()=>{
                         this.removeChild(_model);
                         let energy:energyStore = new energyStore();
                         energy.addEventListener("return",()=>{
                             this.removeChild(energy);
                         },this);
                         this.addChild(energy);
                    },this);
                    this.addChild(_model);
                    return;
                }
                if(this._curGrow == this._maxGrow){
                    Dispatcher.ins.dispatchEvent(new GameEvent("toast","当前植物已成熟，可收取"));
                    return;
                }
                let dataPost = {
                    "plantId":this._plantId,
                    "toUserId":this._toUserId,
                    "wType":0
                }
                socket.request("toWater",dataPost,dataPost.wType);
                break;
            }
            case this.useWater:{
                if(this._curGrow == this._maxGrow){
                    Dispatcher.ins.dispatchEvent(new GameEvent("toast","当前植物已成熟，可收取"));
                    return;
                }
                let dataPost = {
                    "plantId":this._plantId,
                    "toUserId":this._toUserId,
                    "wType":1
                }
                socket.request("toWater",dataPost,dataPost.wType);
                break;
            }
            case this.harvest:{
                let dataPost = {"plantId":this._plantId}
                socket.request("getHarvest",dataPost);
                break;
            }
            case this.toLeft:{
                let max = this._userPlant.length;
                let index = this._index - 1 < 0 ? this._userPlant.length - 1 : this._index - 1;
                let obj = this._userPlant[index];
                let show = "fl_"+obj.PId+"_"+obj.GrowStage+"_png";
                this.anayData(index,obj.PName,show,obj.IndexId,obj.Id,obj.CurGrow,obj.Grow3 + 1,obj.GrowStage,this._node,this._userId);
                break;
            }
            case this.toRight:{
                let index = this._index + 1 > this._userPlant.length - 1 ? 0 : this._index + 1;
                let obj = this._userPlant[index];
                let show = "fl_"+obj.PId+"_"+obj.GrowStage+"_png";
                this.anayData(index,obj.PName,show,obj.IndexId,obj.Id,obj.CurGrow,obj.Grow3 + 1,obj.GrowStage,this._node,this._userId);
                break;
            }
        }
    }
    public anayData(index,name,show,id,plantId,curGrow,maxGrow,growStage,node:number=0,userId:number = 0):void{
        if(this._water && this._water.parent){
            this.removeChild(this._water);
        }
        this._index = index;
        this._name = name;
        this._show = show;
        this._id = id;
        this._plantId = plantId;
        this._curGrow = curGrow;
        this._maxGrow = maxGrow;
        this._growStage = growStage;
        this._userId = userId;
        if(node == 0){
            this._userPlant = common.userPlant;
        }else{
            this._userPlant = common.otherPlant;
            this._toUserId = userId;
        }
        this._node = node;
        this.initData();
        let dataPost:any = {
            id:this._plantId
        }
        if(this._node !== 0){
            this.delete.visible = false;
            this.invite.visible = false;
            this.useEnergy.visible = false;
            dataPost.userId = userId;
        }
    }
    public initData():void{
        this.nickName.text = this._name;
        this.show.source = this._show;
        this.energy.text = this._curGrow + "/" + this._maxGrow;
        this.index.text = (this._index + 1) + "/" + this._userPlant.length;
        this.project.width = 244 * (this._curGrow/this._maxGrow);
        this.useEnergy.label = common.userInfo.energy + ""; 
        this.judgeHarvest();
    }
    public valueEnergy(result):void{
        this._userPlant[this._index].CurGrow = result.CurGrow;
        this._curGrow = result.CurGrow;
        if(this._growStage !== result.GrowStage){
            this._userPlant[this._index].GrowStage = result.GrowStage;
            this._show = "fl_"+this._userPlant[this._index].PId+"_"+this._userPlant[this._index].GrowStage+"_png";
            this.show.source = this._show;
        }
        this.initData();
    }
    public judgeHarvest():void{
        if(this._curGrow == this._maxGrow && this._node == 0){
            this.delete.visible = false;
            this.invite.visible = false;
            this.harvest.visible = true;
        }else{
            this.delete.visible = true;
            this.invite.visible = true;
            this.harvest.visible = false;
        }
        if(this._node !== 0){
            this.delete.visible = false;
            this.invite.visible = false;
            this.useEnergy.visible = false;
            this.addAll.visible = false;
        }
    }
    public assignValue(evt:GameEvent):void{
        let obj = evt.data;
        this.valueEnergy(obj);
        if(common.userInfo.login == 1){
            this.guideToWater.visible = false;
            this.guideToEnergy.visible = true;
            common.userInfo.login = 2;
        }else if(common.userInfo.login == 2){
            this.guideToEnergy.visible = false;
            common.userInfo.login = -1;
            this.closePage();
        }

        if(obj.note == 1){
            if(!this._action){
                var data = RES.getRes("action_json");
                var txtr = RES.getRes("action_png");
                this._action = new egret.MovieClipDataFactory( data, txtr );
            }
            if(!this._water){
                this._water = new egret.MovieClip(  this._action.generateMovieClipData( "water" ) );
                this._water.x = 278;
                this._water.y = 480;
                this._water.scaleX = 0.69;
                this._water.scaleY = 0.69;
                this._water.alpha = 0;
                this._water.addEventListener(egret.Event.COMPLETE, (e:egret.Event)=>{
                    this.removeChild(this._water);
                }, this);
            }
            if(this._time){
                clearTimeout(this._time);
            }
            this._time = setTimeout(()=>{
                if(this._water && this._water.parent){
                    this.removeChild(this._water);
                }
            },1500);
            this._water.gotoAndPlay(1,1);
            egret.Tween.get(this._water).to({alpha:1},200).wait(1600).call(()=>{
                egret.Tween.get(this._water).to({alpha:0},200).call(()=>{
                    this._water.alpha = 0;
                })
            })
            this.addChild(this._water);
        }else{
            if(!this._action){
                var data = RES.getRes("action_json");
                var txtr = RES.getRes("action_png");
                this._action = new egret.MovieClipDataFactory( data, txtr );
            }
            if(!this._energy){
                this._energy = new egret.MovieClip(  this._action.generateMovieClipData( "energy" ) );
                this._energy.x = 278;
                this._energy.y = 480;
                this._energy.scaleX = 0.69;
                this._energy.scaleY = 0.69;
                this._energy.alpha = 0;
            }
            if(this._time){
                clearTimeout(this._time);
            }
            this._time = setTimeout(()=>{
                this.removeChild(this._energy);
            },1000);
            this._energy.gotoAndPlay(1,-1);
            egret.Tween.get(this._energy).to({alpha:1},200).wait(500).call(()=>{
                egret.Tween.get(this._energy).to({alpha:0},200).call(()=>{
                    this._energy.alpha = 0;
                })
            })
          
            this.addChild(this._energy);
        }
        this.energyAction.visible = false;

        this.energyAction.y = 134;
        this.energyAction.alpha = 0;
        this.energyAction.visible = true;
        if(this._energyNote == 0){
            this._energyNote = 1;
            this.actionEnergyNum();
        }else{
            egret.Tween.pauseTweens(this.energyAction);
            this.energyAction.alpha = 0;
            this.energy.y = 134;
            this.actionEnergyNum();
        }
    }
    private actionEnergyNum():void{
        egret.Tween.get(this.energyAction).to({alpha:1,y:98},200).wait(400).call(()=>{
            egret.Tween.get(this.energyAction).to({alpha:0,y:134},300).call(()=>{
                this._energyNote = 0;
            })
        })
    }
    private closePage():void{
        if(this._water && this._water.parent){
            this.removeChild(this._water);
        }
        if(this._node == 0){
            Dispatcher.ins.dispatchEvent(new GameEvent("draw"));
        }else{
            Dispatcher.ins.dispatchEvent(new GameEvent("otherHomeDraw"));
        }

        this.dispatchEventWith("return"); 
    }
    private toGuide():void{
        this.guideGroup.visible = true;
        this.guideToWater.visible = true;
    }
    public setEnergy():void{
        this.useEnergy.label = common.userInfo.energy + ""; 
    }
}